package com.xzk.ExpressInnWeb.util;

import com.xzk.ExpressInnWeb.bean.BootstrapTableUser;
import com.xzk.ExpressInnWeb.bean.User;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

public class UserUtil {

    public static String getUserPhone(HttpSession session){
        Object userphone = session.getAttribute("userphone");
        return (String) userphone;
    }

    public static BootstrapTableUser transferUser(User user) {
        String createtime = DateFormatUtil.format(user.getCreatetime());
        String logintime = user.getLogintime()==null?"未曾登陆":DateFormatUtil.format(user.getLogintime());
        String password = user.getPassword().replaceAll("\\S","*");
        return new BootstrapTableUser(
                user.getId(), user.getUsername(),
                user.getPersonalid(), user.getUserphone(),
                password, createtime, logintime);
    }

    public static List<BootstrapTableUser> transferUserList(List<User> userList){
        List<BootstrapTableUser> btUserList = new ArrayList<>();
        for (User user: userList) {
            btUserList.add(transferUser(user));
        }
        return btUserList;
    }

    public static String getLoginSms(HttpSession session, String phone) {
        return (String) session.getAttribute(phone);
    }

    public static void setLoginSms(HttpSession session, String phone, String code) {
        session.setAttribute(phone, code);
    }

    public static void setWechatUser(HttpSession session, User user) {
        session.setAttribute("wechatUser", user);
    }

    public static User getWechatUser(HttpSession session) {
        Object obj = session.getAttribute("wechatUser");
        if (obj!=null) {
            return (User) obj;
        } else {
            return null;
        }
    }

}
